/**
 * @fileoverview
 * This script defines tests for the flyatlas-service module.
 * @requires YAHOO.log
 * @requires YAHOO.util.Assert
 * @requires YAHOO.tool.TestCase
 * @requires YAHOO.tool.TestSuite
 * @requires YAHOO.tool.TestRunner
 * @requires flyui.util
 * @author <a href="http://purl.org/net/aliman">Alistair Miles</a>
 * @version $Revision:538 $
 * TODO license terms
 */

// Shortcuts
var trace = function(message) { YAHOO.log(message, "test"); };
var assert = YAHOO.util.Assert;


/** 
 * Set up function for the flyui.flyatlas Service.
 */
flyui.flyatlas.Service.setUpTest = function() {
    try {
        trace("setUp test");
    } catch (e) {
        flyui.err("setUp error, "+e.name+", "+e.message);
    }
};


/** 
 * Tear down function for the flyui.flyatlas Service.
 */
flyui.flyatlas.Service.tearDownTest = function() {
    try {
        trace("tearDown test");
    } catch (e) {
        flyui.err("tearDown error, "+e.name+", "+e.message);
    }
};


/** 
 * Test that the contents of the flyui.flyatlas module have been defined.
 */
flyui.flyatlas.Service.testModuleContents = function() {
    
    flyui.info("Test \"==== flyui.flyatlas Service.Tests :: testModuleContents ====\" started.");
    
    assert.isNotUndefined(flyui.flyatlas, "flyui.flyatlas is undefined"); 
    
    assert.isNotUndefined(flyui.flyatlas.Service, "flyui.flyatlas.Service is undefined"); 
    var service = new flyui.flyatlas.Service("foo");
    assert.isNotUndefined(service.query, "service.query is undefined");
    assert.isNotUndefined(service.getAssaysByAffyProbeId, "service.getAssaysByAffyProbeId is undefined");
    assert.isNotUndefined(service.getAssaysByFlybaseGeneId, "service.getAssaysByFlybaseGeneId is undefined");
   
    assert.isNotUndefined(flyui.flyatlas.AssayGroup, "flyui.flyatlas.AssayGroup is undefined");
    var group = new flyui.flyatlas.AssayGroup();
    assert.isNotUndefined(group.affy_id, "group.affy_id is undefined");
    assert.isNotUndefined(group.whole, "group.whole is undefined");
    assert.isNotUndefined(group.brain, "group.brain is undefined");
    assert.isNotUndefined(group.head, "group.head is undefined");
    assert.isNotUndefined(group.crop, "group.crop is undefined");
    assert.isNotUndefined(group.midgut, "group.midgut is undefined");
    assert.isNotUndefined(group.hindgut, "group.hindgut is undefined");
    assert.isNotUndefined(group.tubule, "group.tubule is undefined");
    assert.isNotUndefined(group.ovary, "group.ovary is undefined");
    assert.isNotUndefined(group.testis, "group.testis is undefined");
    assert.isNotUndefined(group.acc, "group.acc is undefined");
    assert.isNotUndefined(group.l_tubule, "group.l_tubule is undefined");
    assert.isNotUndefined(group.l_fatbody, "group.l_fatbody is undefined");
    assert.isNotUndefined(group.ta_ganglion, "group.ta_ganglion is undefined");
    assert.isNotUndefined(group.carcass, "group.carcass is undefined");
    assert.isNotUndefined(group.sgland, "group.sgland is undefined");

    assert.isNotUndefined(flyui.flyatlas.AssayGroup.newInstancesFromResultSet, "flyui.flyatlas.AssayGroup.newInstancesFromResultSet is undefined");
    assert.isNotUndefined(flyui.flyatlas.AssayGroup.newInstanceFromBinding, "flyui.flyatlas.AssayGroup.newInstanceFromBinding is undefined");
    
    assert.isNotUndefined(flyui.flyatlas.Assay, "flyui.flyatlas.Assay is undefined");   
    var assay = new flyui.flyatlas.Assay();
    assert.isNotUndefined(assay.mean, "assay.mean is undefined");   
    assert.isNotUndefined(assay.present, "assay.present is undefined");   
    assert.isNotUndefined(assay.sem, "assay.SEM is undefined");   
    assert.isNotUndefined(assay.change, "assay.change is undefined");   
    assert.isNotUndefined(assay.ratio, "assay.ratio is undefined");   

    assert.isNotUndefined(flyui.flyatlas.Assay.newInstanceFromBinding, "flyui.flyatlas.Assay.newInstanceFromBinding is undefined");
        
};


/** 
 * Test converting sparql results to assaygroup.
 */
flyui.flyatlas.Service.testParseSPARQLResultSetSingleProbe = function() {
    flyui.info("Test \"==== flyui.flyatlas.Service Tests :: testParseSPARQLResultSetSingleProbe ====\" started.");
	
	var groups = flyui.flyatlas.AssayGroup.newInstancesFromResultSet(resultSet_1616608_a_at);
	assert.isNotNull(groups, "groups is null");
	assert.areEqual(1, groups.length, "expected 1 group");
	
	var group = groups[0];
	
	assert.areEqualAssayGroups(assaygroup_1616608_a_at, group);
	
};


flyui.flyatlas.Service.testQuery = function( endpoint, runner ) {
    flyui.info("Test \"==== flyui.flyatlas.Service Tests :: testQuery ====\" started.");

	var service = new flyui.flyatlas.Service();
	service.setEndpoint(endpoint);
	
	function success(o) {
		trace("success case, resume runner");
		runner.resume(function() {
            // check status code            
            assert.areEqual(200, o.status);
            
            // try parsing response text as json
            var resultSet = YAHOO.lang.JSON.parse(o.responseText);
            
            // check json content
            assert.isTrue(resultSet["boolean"], "result set not as expected");
		});
	}
	
	function failure(o) {
        trace("failure case, resume runner");
        runner.resume(function() {
            assert.fail("request failed, status "+o.status+" "+o.statusText);
        });
	}
	
	var query = "ASK { ?s ?p ?o }";
	
	trace("make call");
	service.query(query, success, failure);
	
	trace("suspend runner, if test hangs here there is a problem")
	runner.wait();
	
	assert.fail("this should never be reached");
	
};


flyui.flyatlas.Service.testGetAssaysByAffyProbeId = function( id, expected, endpoint, runner ) {
    flyui.info("Test \"==== flyui.flyatlas.Service Tests :: testGetAssaysByAffyProbeId ====\" started.");

    var service = new flyui.flyatlas.Service();
    service.setEndpoint(endpoint);
    
    function success(groups) {
        trace("success case, resume runner");
        runner.resume(function() {
        	assert.areEqual(1, groups.length, "expected 1 assay group");
        	assert.areEqualAssayGroups(expected, groups[0]);
    	});
    }

    function failure(o) {
        trace("failure case, resume runner");
        runner.resume(function() {
            assert.fail("request failed, status "+o.status+" "+o.statusText);
        });
    }

    trace("make call");
    try {
	    service.getAssaysByAffyProbeId(id, success, failure);
    } catch (e) {
    	flyui.err("caught exception: "+e.name+" "+e.message);
    	for (var p in e) { trace(p+" : "+e[p]); }
    	assert.fail("unexpected error, may mean exceed url length? N.B. IE max length 2083.");
    	// ie error code -2147467259
    }
    
    trace("suspend runner, if test hangs here there is a problem")
    runner.wait();
    
    assert.fail("this should never be reached");
    
}

flyui.flyatlas.Service.testGetAssaysByFlybaseGeneId = function( id, expected, endpoint, runner ) {
    flyui.info("Test \"==== flyui.flyatlas.Service Tests :: testGetAssaysByFlybaseGeneId :: "+id+" ====\" started.");

    var service = new flyui.flyatlas.Service();
    service.setEndpoint(endpoint);
    
    function success(groups) {
        trace("success case, resume runner");
        runner.resume(function() {
        	assert.areEqual(expected.length, groups.length, "number of assay groups not as expected");
        	for (var i=0; i<expected.length; i++) {
        		var expect = expected[i];
        		var expect_affy = expect.affy_id;
        		trace("looking for expected group for "+expect_affy);
        		var found = false;
        		for (var j=0; j<groups.length; j++) {
        			var group = groups[j];
        			var affy = group.affy_id;
        			if (expect_affy == affy) {
        				found = true;
			        	assert.areEqualAssayGroups(expect, group);
        			}
        		}
        		assert.isTrue(found, "did not find group for "+expect_affy);
        	}
    	});
    }

    function failure(o) {
        trace("failure case, resume runner");
        runner.resume(function() {
            assert.fail("request failed, status "+o.status+" "+o.statusText);
        });
    }

    trace("make call");
    try {
	    service.getAssaysByFlybaseGeneId(id, success, failure);
    } catch (e) {
    	flyui.err("caught exception: "+e.name+" "+e.message);
    	for (var p in e) { trace(p+" : "+e[p]); }
    	assert.fail("unexpected error, may mean exceed url length? N.B. IE max length 2083.");
    	// ie error code -2147467259
    }
    
    trace("suspend runner, if test hangs here there is a problem")
    runner.wait();
    
    assert.fail("this should never be reached");
    
}


// ************************************************************
// TEST CASE
// ************************************************************


/** 
 * Construct a YUI test case for the flyui.flyatlas Service.
 * @return a YUI test case
 * @type YAHOO.tool.TestCase
 */
flyui.flyatlas.Service.TestCase = function( endpointURL ) {
    
    var testCase = new YAHOO.tool.TestCase({
        name: "=== flyui.flyatlas Service.Tests ===",
        setUp : flyui.flyatlas.Service.setUpTest,
        tearDown : flyui.flyatlas.Service.tearDownTest,
        testModuleContents : flyui.flyatlas.Service.testModuleContents,
        testParseSPARQLResultSetSingleProbe : flyui.flyatlas.Service.testParseSPARQLResultSetSingleProbe,
        testQuery : function() {
        	flyui.flyatlas.Service.testQuery(endpointURL, this);
        },
        testGetAssaysByAffyProbeId_1616608_a_at : function() {
            flyui.flyatlas.Service.testGetAssaysByAffyProbeId("1616608_a_at", assaygroup_1616608_a_at, endpointURL, this);
        },
        testGetAssaysByFlybaseGeneId_foo : function() {
            flyui.flyatlas.Service.testGetAssaysByFlybaseGeneId("foo", [], endpointURL, this);
        },
        testGetAssaysByFlybaseGeneId_FBgn0001128 : function() {
        	// FBgn0001128 maps to 1616608_a_at, 1625949_at, 1634893_at, 1636311_at
//        	var expected = [assaygroup_1616608_a_at, assaygroup_1634893_at, assaygroup_1636311_at, assaygroup_1625949_at];
			var expected = flyui.flyatlas.AssayGroup.newInstancesFromResultSet(resultset_FBgn0001128);
            flyui.flyatlas.Service.testGetAssaysByFlybaseGeneId("FBgn0001128", expected, endpointURL, this);
        }
    });
    
    return testCase;
    
};

 
// ************************************************************
// TEST SUITE & RUNNER
// ************************************************************


/** 
 * Construct a YUI test suite for the flyui.flyatlas module.
 * @param {String} endpointURL URL of service
 * @return a YUI test suite
 * @type YAHOO.tool.TestSuite
 */
flyui.flyatlas.Service.TestSuite = function( endpointURL ) {
    var suite = new YAHOO.tool.TestSuite("== flyui.flyatlas Test Suite ==");
    suite.add(flyui.flyatlas.Service.TestCase(endpointURL));
    return suite;
}

/** 
 * Run the flyui.flyatlas test suite.
 * @param {String} endpointURL URL of service
 */
flyui.flyatlas.Service.runTests = function( endpointURL ) {
    trace("flyui.flyatlas :: running tests");
    YAHOO.tool.TestRunner.clear();
    YAHOO.tool.TestRunner.add(flyui.flyatlas.Service.TestSuite(endpointURL));
    YAHOO.tool.TestRunner.run();
}
 